package com.ruoyi.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.admin.domain.dto.FlawDto;
import com.ruoyi.admin.domain.entity.WaybillModel;
import com.ruoyi.admin.domain.vo.response.waybill.MonthlyLossVo;
import com.ruoyi.admin.domain.vo.response.waybill.WaybillPageResponse;
import com.ruoyi.admin.domain.vo.response.waybill.WaybillResponse;
import com.ruoyi.admin.mapper.FlawMapper;
import com.ruoyi.admin.mapper.ReturnGoodsMapper;
import com.ruoyi.admin.mapper.WaybillMapper;
import com.ruoyi.admin.service.WaybillService;
import com.ruoyi.common.api.Result;
import com.ruoyi.common.api.ResultCode;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 快递(Waybill)Service实现类
 *
 * @author jianhan
 * @date 2022-12-07 15:41:41
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WaybillServiceImpl extends ServiceImpl<WaybillMapper, WaybillModel> implements WaybillService {

    private static final Logger logger = LoggerFactory.getLogger(WaybillServiceImpl.class);

    @Resource
    private FlawMapper flawMapper;

    @Resource
    private ReturnGoodsMapper returnGoodsMapper;

    @Override
    public Result<WaybillResponse> getId(Long waybillId) {
        WaybillModel waybillModel = this.getById(waybillId);
        if (waybillModel != null) {
            WaybillResponse waybillResponse = new WaybillResponse(waybillModel);
            return Result.success(waybillResponse);
        }
        return Result.failed(ResultCode.DATA_NOT_EXIST);
    }

    @Override
    public Result<WaybillPageResponse> page(Page<WaybillModel> page, WaybillModel searchModel) {
        QueryWrapper<WaybillModel> queryWrapper = queryWrapper(searchModel);

        Page<WaybillModel> pages = this.page(page, queryWrapper);
        WaybillPageResponse waybillPageResponse = new WaybillPageResponse(pages);
        page.getRecords().forEach((waybillModel) -> waybillPageResponse.getRecords().add(new WaybillResponse(waybillModel)));
        return Result.success(waybillPageResponse);

    }

    @Override
    public Result add(WaybillModel waybillModel) {
        Boolean result = this.save(waybillModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("添加失败");
    }

    @Override
    public Result update(WaybillModel waybillModel) {
        Boolean result = this.updateById(waybillModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("更新失败");
    }

    @Override
    public Result lose(){
        MonthlyLossVo monthlyLossVo = new MonthlyLossVo();
        FlawDto flawCount = flawMapper.getFlawCount();
        monthlyLossVo.setFlawCost(flawCount.getFlawCost());
        monthlyLossVo.setFlawTotal(flawCount.getFlawTotal());
        BigDecimal bigDecimal = baseMapper.freightCount();
        monthlyLossVo.setFreight(bigDecimal);
        monthlyLossVo.setReturnNumber(returnGoodsMapper.getReturnNumTotal());
        return Result.success(monthlyLossVo);
    }

    @Override
    public Result delete(Long waybillId) {
        Boolean result = this.removeById(waybillId);
        return Result.judge(result);

    }

    private QueryWrapper<WaybillModel> queryWrapper(WaybillModel waybillModel) {
        QueryWrapper<WaybillModel> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(waybillModel.getWaybillNumber())){
            queryWrapper.like("waybill_number",waybillModel.getWaybillNumber());
        }
        if (StringUtils.isNotEmpty(waybillModel.getType())){
            queryWrapper.eq("type",waybillModel.getType());
        }
        if (StringUtils.isNotEmpty(waybillModel.getSchedulingIds())){
            queryWrapper.apply("find_in_set( "+waybillModel.getSchedulingIds()+", scheduling_ids)");
        }
        if (StringUtils.isNotEmpty(waybillModel.getReturnIds())){
            queryWrapper.like("return_ids",waybillModel.getReturnIds());
        }
        /*
           写搜索条件
        */
        queryWrapper.orderByDesc("id");
        return queryWrapper;
    }

}
